<template>
    <i-select v-model="text" @on-change='selectChange' filterable :disabled='pDisabled' :clearable='pClearable'><!--label-in-value-->
        <i-option v-for="item in list" :value="item[pKeyText]" :key="item[pValueText]">{{ item[pValueText] }}</i-option>
    </i-select>
</template>
<script>
    import {GET_STATUS,GET_FAR_STATUS,GET_LAZY_STATUS} from 'serves/data-status'
    export default{
        name:'StatusSelect',
        props:{
            pData:{
                type:[String,Number],
                request:true
            },
            pName:{
                type:String,
                request:true
            },
            pDisabled:{
                type:Boolean,
                request:false
            },
            pKeyText:{
                type:String,
                default:'code'
            },
            pValueText:{
                type:String,
                default:'name'
            },
            pLazy:{
                type:Boolean,
                default:false
            },
            pLazyUpdate:{
                type:Boolean,
                default:false
            },
            pDef:{
                type:[Boolean,String],
                default:false
            },
            pFar:{
                type:Boolean,
                default:false
            },
            pClearable:{
                type:Boolean,
                default:true
            },
            pDel:{
                type:Array,
                default:function () {
                    return [];
                }
            },
        },
        data(){
            return{
                text:'',
                list:[],
            }
        },
        created(){
            this.init();
        },
        methods:{
            initText(val){
                this.$set(this,'text',val||'');
            },
            setData(d){
                d = Object.assign([],d);
                if(this.pDel.length>0){
                    d = d.filter((v,i)=>{
                        let z = true;
                        this.pDel.forEach((x,y)=>{
                            if(v[this.pKeyText]==x){
                                z = false;
                            }
                        });
                        return z;
                    });
                }
                this.$set(this,'list',d);
                this.initText(this.pData);
                this.defChange()
            },
            init(){
                if(this.pFar){
                    this.setData(GET_FAR_STATUS(this.pName,false,this.pKeyText));
                }else {
                    if(!this.pLazy){
                        this.setData(GET_STATUS(this.pName,false,this.pKeyText));
                    }else {
                        GET_LAZY_STATUS(this.pName,false,this.pKeyText,this.pLazyUpdate).then((res)=>{
                            this.setData(res);
                        });
                    }
                }
            },
            selectChange(value){
                if(value==undefined){value = ''}
                this.$emit('on-change',value)
            },
            defChange(){
                if(this.pDef&&this.pData === ''){
                    if(typeof this.pDef == 'string'){
                        let a = [];
                        this.list.forEach((v,i)=>{
                            if(v[this.pKeyText]==this.pDef){
                                a.push(v)
                            }
                        })
                        if(a.length==1){
                            this.selectChange(a[0][this.pKeyText])
                        }else {
                            this.selectChange(this.list[0]&&this.list[0][this.pKeyText])
                        }
                    }else {
                        this.selectChange(this.list[0]&&this.list[0][this.pKeyText])
                    }
                }
            }
        },
        watch:{
            pData(newVal,olVal){
                if(newVal!=this.text){
                    this.initText(newVal)
                }
                if(newVal==''){
                    this.defChange();
                }
            }
        },
        components:{

        }
    }
</script>